package com.pphh.demo;

import com.pphh.demo.exception.BlockException;

import java.util.concurrent.Semaphore;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 11/21/2018
 */
public class SemaphoreAsynFlowControl extends FlowControl {

    private static Semaphore semaphore = new Semaphore(5);

    @Override
    public void run() throws BlockException {
        if (semaphore.tryAcquire()) {
            // execute the test task
            test();
            semaphore.release();
        } else {
            throw new BlockException("task is blocked");
        }
    }

}
